/**
 * @开发版权 
 * @项目名称 轻量数据中台(LDMP)
 * @版本信息 v1.0
 * @开发人员 zhous
 * @开发日期 2025-01-17
 * @修订日期
 * @描述  MenuController
 */
package com.ng.sys.controller;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.jfinal.kit.StrKit;
import com.ng.common.annotation.TokenRequired;
import com.ng.common.annotation.Tx;
import com.ng.common.controller.Controller;
import com.ng.common.enums.MenuTypeEnum;
import com.ng.common.model.response.R;
import com.ng.common.util.UuidUtil;
import com.ng.sys.model.domain.*;
import com.ng.sys.model.response.RouteResponse;
import com.ng.sys.service.IMenu;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;

@Tag(name = "菜单管理")
@RequiredArgsConstructor
@RestController
@RequestMapping("/sys/menu")
public class MenuController extends Controller{

	@Autowired
	private IMenu menuImpl;

	@Operation(summary = "路由列表")
	@Parameters({
	    @Parameter(name = "keywords", description = "关键字(名称)", required = false)
	})
	@TokenRequired
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public R<List<Menu>> list(HttpServletRequest request,
			@RequestParam(name = "keywords", required = false, defaultValue = "") String keywords) {
		String _sql = "select * from " + Menu.TABLE_NAME + " where 1=1 ";
		if (!StrKit.isBlank(keywords)) {
			_sql += " and name like '%" + keywords + "%' ";
		}
		_sql += " order by sort asc";
		return R.ok(Menu.dao.find(_sql));
	}

	@Operation(summary = "当前用户系统菜单")
	@Parameters({
	    @Parameter(name = "flag", description = "系统标识(0-内部菜单|1外部菜单)", required = false)
	})
	@TokenRequired
	@RequestMapping(value = "/routes", method = RequestMethod.GET)
	public R<List<RouteResponse>> routes(HttpServletRequest request,@RequestParam(name = "flag", required = false, defaultValue = "0") String flag) {
		try {
			return R.ok(menuImpl.routes(this.getUserId(request),flag));
		} catch (Exception e) {
			e.printStackTrace();
			return R.failed(10042, "会话过期，请重新登录！");
		}
	}

	@Operation(summary = "保存菜单")
	@Parameters({
	    @Parameter(name = "menu", description = "菜单", required = false)
	})
	@Tx
	@TokenRequired
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public R<Menu> save(HttpServletRequest request,@RequestBody Menu menu) {
		if (StrKit.isBlank(menu.getId())) {
			if(StrKit.isBlank(menu.getComponent())) {
				if(StrKit.equals(MenuTypeEnum.CATALOG.getType(), menu.getType())){
					menu.setComponent("Layout");
				}
			}
			menu.setId(UuidUtil.getUUID());
			if(StrKit.isBlank(menu.getParentId())) {
				menu.setParentId("-1");
			}
			if (menu.save())
				return R.ok();
		}
		if(StrKit.isBlank(menu.getParentId())) {
			menu.setParentId("-1");
		}
		
		if(StrKit.isBlank(menu.getComponent())) {
			if(StrKit.equals(MenuTypeEnum.CATALOG.getType(), menu.getType())){
				menu.setComponent("Layout");
			}
		}
		if (menu.update())
			return R.ok();
		return R.failed();
	}

	@Operation(summary = "删除菜单")
	@Parameters({
	    @Parameter(name = "ids", description = "菜单id集合(逗号分隔)", required = false)
	})
	@DeleteMapping("/{ids}")
	@TokenRequired
	public R<String> deleteMenu(@PathVariable("ids") String ids) {
		
		String[] arr=ids.split(",");
		if(ids==null || ids.length()<=0) {
			return R.failed("请选择需要删除的角色.");
		}
		for(String id:arr) {
			menuImpl.delete(id);
		}
		
		return R.ok();
	}
	
	@Operation(summary = "菜单下拉列表")
    @GetMapping("/options")
    public R<List<Menu>> listMenuOptions() {
        return R.ok(menuImpl.listMenuOptions());
    }

}
